package com.tanhua.admin.controller;

/**
 * @Author: cl
 * @Date: 2022/5/17 - 05 - 17
 * @Description:
 */

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.service.AnalysisService;
import com.tanhua.model.vo.AnalysisSummaryVo;
import com.tanhua.model.vo.AnalysisUsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 后台展示用户数据分析模块
 */
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private AnalysisService analysisService;

    /**
     * 获取概要统计信息
     *
     * @return
     */
    @GetMapping("/summary")
    public ResponseEntity getSummary() {

        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

        //1、获取当前时间
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        DateTime dateTime = DateUtil.parseDate("2020-7-1");//hutool工具类

        //2、累计用户数
        analysisSummaryVo.setCumulativeUsers(analysisService.getAllUserCount());//获取总用户方法

        //3、过去30天活跃用户
        analysisSummaryVo.setActivePassMonth(analysisService.getActiveUserCount(dateTime, -30));//获取时间段活跃用户

        //4、过去7天活跃用户
        analysisSummaryVo.setActivePassWeek(analysisService.getActiveUserCount(dateTime, -7));

        //5、今日活跃用户
        analysisSummaryVo.setActiveUsersToday(analysisService.getActiveUserCountDay(dateTime, 0));//获取指定时间活跃用户

        //6、活跃用户的涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setActiveUsersTodayRate(computeRate(
                analysisSummaryVo.getActiveUsersToday(),//今日活跃用户数
                analysisService.getActiveUserCountDay(dateTime, -1)//昨日活跃用户数
        ));

        //7、今日新增用户
        analysisSummaryVo.setNewUsersToday(analysisService.getRegisterUserCount(dateTime, 0));//获取指定时间新增用户

        //8、新增用户涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setNewUsersTodayRate(computeRate(
                analysisSummaryVo.getNewUsersToday(),//今日新增用户数
                analysisService.getRegisterUserCount(dateTime, -1)//昨日新增用户数
        ));

        //9、今日登录次数
        analysisSummaryVo.setLoginTimesToday(analysisService.getLoginUserCount(dateTime, 0));//获取指定时间登录用户数

        //10、今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setLoginTimesTodayRate(computeRate(
                analysisSummaryVo.getLoginTimesToday(),//今日登录次数
                analysisService.getLoginUserCount(dateTime, -1)  //昨日登陆次数
        ));

        return ResponseEntity.ok(analysisSummaryVo);
    }

    //计算环比增长率
    private static BigDecimal computeRate(Long current, Long last) {
        BigDecimal result;
        if (last == 0) {
            // 当上一期计数为零时，此时环比增长为倍数增长
            result = new BigDecimal((current - last) * 100);
        } else {
            result = BigDecimal.valueOf((current - last) * 100).divide(BigDecimal.valueOf(last), 2, BigDecimal.ROUND_HALF_DOWN);
        }
        return result;
    }


    /**
     * 新增、活跃用户、次日留存率
     * 演示图形报表时选择时间段：2020-09-01至2020-10-01
     *
     * @param sd   开始时间
     * @param ed   结束时间
     * @param type 101 新增 102 活跃用户 103 次日留存率
     * @return 新增 or 活跃用户 or 次日留存率
     */
    @GetMapping("/users")
    public AnalysisUsersVo getUsers(@RequestParam(name = "sd") Long sd
            , @RequestParam("ed") Long ed
            , @RequestParam("type") Integer type) {
        return this.analysisService.getAnalysisUsersVo(sd, ed, type);
    }
}
